package UI

import groovy.swing.SwingBuilder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import feed.*

if(System.getProperty("notify") == "visible")
{
	println "Notify is already on"
	return 
}

swing = new SwingBuilder()

public class ContentComponent extends JComponent  {
    def textToShow
    def title = "Programmer's RSS Reader"
    def alpha = 1.0F

private AlphaComposite makeComposite(float alpha) {
    int type = AlphaComposite.SRC_OVER;
    return(AlphaComposite.getInstance(type, alpha));
  }
  

    public void paint(Graphics g){
        setOpaque(false);
        // Add an empty border around us to compensate for
        // the rounded corners.
        setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5))
        super.paint(g)

      Graphics2D g2d = (Graphics2D)g;
      g2d.setComposite(makeComposite(alpha));

      Color s2 = Color.cyan;
      Color e1 = Color.white;
      GradientPaint gradient1 = new GradientPaint(0,0,s2,200,200,e1,true);
      g2d.setPaint(gradient1);
      g2d.fillRoundRect(0,0,200,200,40,40);
      //g2d.fillRect(0,0,200,200);
      //def img = ImageIO.read(new File('icon_star_full.gif'));
      //g2d.drawImage(img, 180, 0,20,20, null);
      g2d.setColor(Color.black)
      def stroke = new BasicStroke (4.0F, BasicStroke.CAP_ROUND, 
                                  BasicStroke.JOIN_BEVEL);

      g2d.setStroke(stroke)
      
      g2d.drawLine(185,10,195,20)
      g2d.drawLine(195,10,185,20)
      def font = g2d.getFont()
      def boldFont = font.deriveFont(Font.BOLD)
      g2d.setFont(boldFont)
      g2d.drawString(title,5,20)
      g2d.setFont(font)
      def y = 40
      for(int i=0;i<textToShow.size();i++)
      {
          g2d.drawString(textToShow[i],5,y)
      y+=15
      }
    }
  }

//TODO: Calculate size based on monitor
def final frame = swing.frame(
    title:'Programmer\'s RSS reader', 
    location:[100,100], 
    size:[200,200], 
        defaultCloseOperation:WindowConstants.EXIT_ON_CLOSE, 
    undecorated:true) 
    
    
  def screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
  def height = 200
  def width = 200
  
  frame.setBounds((int)(screenDimension.width-width-20),(int)(screenDimension.height-height-20),width,height)
  contentComponent = new ContentComponent()
  def feedStream = new FeedStream()
def nextFeed={ 
	def s = it.next();
        if(s.length()>29)
        {
		s = s.substring(0, 27)+"..."
        }
        s
}
	
  contentComponent.textToShow = [
    nextFeed(feedStream) ,
    nextFeed(feedStream),
    nextFeed(feedStream),
    nextFeed(feedStream),
    nextFeed(feedStream),
    nextFeed(feedStream),
    nextFeed(feedStream),
    nextFeed(feedStream),
    nextFeed(feedStream),
    nextFeed(feedStream)
]

class CloseMouseAdapter extends MouseAdapter
{
    def frame
    def component
        public void mouseClicked(MouseEvent e) {
		System.setProperty("notify","hidden")
                frame.dispose();
                (new Reader()).run()
                //System.out.println("MouseListenerDemo.mouseClicked");
            }
        public void mouseEntered(MouseEvent e) {
                component.alpha = 1.0F
                component.repaint()
                //System.out.println("MouseListenerDemo.mouseClicked");
            }
        public void mouseExited(MouseEvent e) {
                component.alpha = 0.4F
                component.repaint()
                //System.out.println("MouseListenerDemo.mouseClicked");
            }
}
def adaptor = new CloseMouseAdapter() 
adaptor.frame = frame
adaptor.component = contentComponent
contentComponent.addMouseListener(adaptor);
frame.getContentPane().add(contentComponent);
//frame.pack()
frame.setVisible(true)
frame.show()
System.setProperty("notify","visible")

